package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.entity.R;
import com.hzit.entity.TbTypeTemplate;
import com.hzit.mapper.TbTypeTemplateMapper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/template")
public class TemplateServlet extends HttpServlet {
	private TbTypeTemplateMapper typeTemplateMapper;
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		typeTemplateMapper = wac.getBean(TbTypeTemplateMapper.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 处理中文乱码
		resp.setContentType("text/html;charset=utf-8");
		//2. 得到请求参数
		String cmd = req.getParameter("cmd");
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
			if("add".equals(cmd)){
				add(req,resp);
			}
			if("update".equals(cmd)){
				update(req,resp);
			}
			if("delete".equals(cmd)){
				delete(req,resp);
			}
		}
	}

	//2. 列表模板
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 查询模板列表
		List<TbTypeTemplate> templates = typeTemplateMapper.selectByExample(null);
		//2.2 放到r对象中
		R r = R.ok().put("templates",templates);
		//2.3 输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();

	}
	//3. 添加模板
	private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		String temp = req.getParameter("temp");
		System.out.println("temp = " + temp);
		TbTypeTemplate tbTypeTemplate = JSON.parseObject(temp, TbTypeTemplate.class);
		System.out.println("tbTypeTemplate = " + tbTypeTemplate);

		R r = null;
		try{
			typeTemplateMapper.insert(tbTypeTemplate);
			r = R.ok("添加成功！");
		}catch (Exception e){
			r = R.error("添加失败！");
		}
		//3.3 输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}
	//4. 修改模板
	private void update(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		String temp = req.getParameter("temp");
		System.out.println("temp = " + temp);
		TbTypeTemplate tbTypeTemplate = JSON.parseObject(temp, TbTypeTemplate.class);
		System.out.println("tbTypeTemplate = " + tbTypeTemplate);

		R r = null;
		try{
			typeTemplateMapper.updateByPrimaryKey(tbTypeTemplate);
			r = R.ok("修改成功！");
		}catch (Exception e){
			r = R.error("修改失败！");
		}
		//4.3 输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}
	//5. 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//5.1 得到要删除的模板编号
		String id = req.getParameter("id");
		R r = null;
		try{
			//5.2 删除模板
			typeTemplateMapper.deleteByPrimaryKey(new Long(id));
			r = R.ok("删除成功！");
		}catch (Exception e){
			r = R.error("删除失败！");
		}
		//5.3 输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}

}
